package data;

import ihm.FenetrePrincipale;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JLabel;

import database.ConnexionDB;

public class TimerListener implements ActionListener,Observer {
    private int heure;
	private int minute;
    private int seconde;
    private Chrono chrono;
    Partie p1 = new Partie();
    Temps t1; 
    private ConnexionDB DB;
    
    public TimerListener(Chrono chrono,ConnexionDB db) {
        super();
        this.DB=db;
        this.chrono = chrono;
  }
    
    public TimerListener(Chrono chrono,int heu,int min, int sec) {
          super();
          this.chrono = chrono;
          this.heure = heu;
          this.minute = min;
          this.seconde = sec;
          
    }

    public void actionPerformed(ActionEvent e) {
    	if(! chrono.is_pause()){
	    	if (this.minute == 0 && this.seconde == 0) 
	    	{
	            this.heure--;
	            this.minute = 59;
	            this.seconde = 59;
	    	}
	    	else if (this.seconde == 0)  
	    	{
	    		this.minute--;
	            this.seconde = 59;
	        }
	    	else
	           this.seconde--;
	    	
	          //lblChron.setText(this.heure + " : " + this.minute + " : " + this.seconde);
	    	Temps t = new Temps(this.heure,this.minute,this.seconde);
	    	this.chrono.setChrono(t);
    	}
    }

	@Override
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub
		ConnexionDB DB = (ConnexionDB) o;
		String requeteSelectUsers = "SELECT * FROM partie";
      	 
        ResultSet resultat; 
			
        try {
			resultat = DB.executerRequete(requeteSelectUsers);
			while(resultat.next()){
	               p1.setId_utilisateur(resultat.getInt("id_utilisateur"));
	               p1.setChrono_general(resultat.getLong("chrono_general"));
	               t1 = new Temps(p1.getChrono_general());
	               this.heure = t1.getHeures();
	               this.minute = t1.getMinutes();
	               this.seconde = t1.getSecondes();
	           }
			//System.out.println("[Admin][TimerListener] chrono update: "+t1.conversion()+" secondes");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
 